﻿#region Copyright
// Copyright (c) 2010, Kazi Manzur Rashid <kazimanzurrashid@gmail.com>.
// This source is subject to the Microsoft Public License. 
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL. 
// All other rights reserved.
#endregion

namespace LiveWriter.Plugins.SyntaxHighlighter
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Windows.Forms;

    using WindowsLive.Writer.Api;

    [WriterPlugin("9B27ED64-9D73-4d3e-9885-F57E703EC18C", "Syntax Highlighter", Description = "Code Snippet based upon popular Syntax Highlighter(http://alexgorbatchev.com/SyntaxHighlighter/)", HasEditableOptions = true, PublisherUrl = Constants.Home)]
    [InsertableContentSourceAttribute("Insert Code", SidebarText = "Insert Code")]
    public class Plugin : SmartContentSource
    {
        public override void EditOptions(IWin32Window dialogOwner)
        {
            using (DefaultSetting form = new DefaultSetting(Options))
            {
                form.ShowDialog(dialogOwner);
            }
        }

        public override DialogResult CreateContent(IWin32Window dialogOwner, ISmartContent newContent)
        {
            newContent.Properties.Language(Options.Language());
            newContent.Properties.AutoLinks(Options.AutoLinks());
            newContent.Properties.Collapse(Options.Collapse());
            newContent.Properties.SmartTabs(Options.SmartTabs());
            newContent.Properties.Gutter(Options.Gutter());
            newContent.Properties.Toolbar(Options.Toolbar());
            newContent.Properties.HtmlScript(Options.HtmlScript());
            newContent.Properties.TabSize(Options.TabSize());

            using (CodeEditor form = new CodeEditor{ Properties = newContent.Properties })
            {
                return form.ShowDialog(dialogOwner);
            }
        }

        public override string GenerateEditorHtml(ISmartContent content, IPublishingContext publishingContext)
        {
            return GenerateHtml(content);
        }

        public override string GeneratePublishHtml(ISmartContent content, IPublishingContext publishingContext)
        {
            return GenerateHtml(content);
        }

        public override SmartContentEditor CreateEditor(ISmartContentEditorSite editorSite)
        {
            return new EditSetting<CodeEditor>();
        }

        private static string GenerateHtml(ISmartContent content)
        {
            IProperties properties = content.Properties;

            string formattedCode = properties.Code().FormattedCode();

            string figCaption = string.Empty;

            if (!string.IsNullOrEmpty(properties.Caption()))
            {
                figCaption = string.Format(CultureInfo.InvariantCulture, "<figcaption>{0}</figcaption>{1}", content.Properties.Caption(), Environment.NewLine);
            }

            List<string> optionList = new List<string>();

            Action<string, string> append = (key, value) => optionList.Add(string.Format(CultureInfo.InvariantCulture, "{0}: {1}", key, value));

            append("brush", properties.Language());

            if (!properties.AutoLinks())
            {
                append("auto-links", "false");
            }

            if (properties.Collapse())
            {
                append("auto-links", "true");
            }

            if (properties.FirstLine() != 1)
            {
                append("first-line", properties.FirstLine().ToString(CultureInfo.InvariantCulture));
            }

            if (!properties.Gutter())
            {
                append("gutter", "false");
            }

            if (properties.HtmlScript())
            {
                append("html-script", "true");
            }

            if (!properties.SmartTabs())
            {
                append("smart-tabs", "false");
            }

            if (properties.TabSize() != 4)
            {
                append("tab-size", properties.TabSize().ToString(CultureInfo.InvariantCulture));
            }

            if (properties.Toolbar())
            {
                append("toolbar", "true");
            }

            if (!string.IsNullOrEmpty(properties.HighlightLines()))
            {
                append("highlight", "[" + properties.HighlightLines() + "]");
            }

            string options = string.Join("; ", optionList.ToArray());

            string html = string.Format(CultureInfo.InvariantCulture, "{0}<figure>{0}{1}<pre><code class=\"{2} language-{3}\">{0}{4}{0}</code></pre>{0}</figure>{0}", Environment.NewLine, figCaption, options, properties.Language(), formattedCode);

            return html;
        }
    }
}